package com.pts.dao;

import com.pts.db.DbUtils;
import com.pts.vo.Produce;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;

public class ProduceDao extends GenericDao {
    @Override
    public Object findById(int id){
        Produce produce = new Produce();
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        ResultSet resultSet;
        String sql="select id,eid,card,date,enddate,place,price,tel,pername from produce where id=?";
        try {
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            if(!resultSet.next()){
                resultSet.close();
                preparedStatement.close();
                return null;
            }
            produce.setId(resultSet.getInt("id"));
            produce.setEid(resultSet.getInt("eid"));
            produce.setCard(resultSet.getString("card"));
            produce.setDateTime(resultSet.getDate("date"));
            produce.setEndDate(resultSet.getDate("enddate"));
            produce.setName(resultSet.getString("pername"));
            produce.setPlace(resultSet.getString("place"));
            produce.setTel(resultSet.getString("tel"));
            produce.setPrice(resultSet.getFloat("price"));
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return produce;
    }

    //添加信息
    @Override
    public boolean add(Object obj){
        Produce produce = (Produce) obj;
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        String sql = "insert into produce(eid,card,date,enddate,place,price,tel,pername) values (?,?,?,?,?,?,?,?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,String.valueOf(produce.getEid()));
            preparedStatement.setString(2,produce.getCard());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            preparedStatement.setString(3,simpleDateFormat.format(produce.getDateTime()));
            preparedStatement.setString(4,simpleDateFormat.format(produce.getEndDate()));
            preparedStatement.setString(5,produce.getPlace());
            preparedStatement.setString(6,String.valueOf(produce.getPrice()));
            preparedStatement.setString(7,produce.getTel());
            preparedStatement.setString(8,produce.getName());
            int flag = preparedStatement.executeUpdate();
            if(flag==0){
                preparedStatement.close();
                return false;
            }
            preparedStatement.close();
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        return true;
    }

    //查找id
    @Override
    public int getId(Object obj){
        int flag = 0;
        Produce produce = (Produce) obj;
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        ResultSet resultSet;
        String sql="select id from produce where card=?";
        try {
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,((Produce) obj).getCard());
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()) {
                flag = resultSet.getInt("id");
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    //修改信息
    public boolean update(Object obj){
        Produce produce = (Produce) obj;
        DbUtils du = new DbUtils();
        Connection connection = du.getConn();
        String sql = "update produce eid = ?,card = ?,date = ?,enddate = ?,place = ?,price = ?,tel = ?,pername = ?) where id = ?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,String.valueOf(produce.getEid()));
            preparedStatement.setString(2,produce.getCard());
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            preparedStatement.setString(3,simpleDateFormat.format(produce.getDateTime()));
            preparedStatement.setString(4,simpleDateFormat.format(produce.getEndDate()));
            preparedStatement.setString(5,produce.getPlace());
            preparedStatement.setString(6,String.valueOf(produce.getPrice()));
            preparedStatement.setString(7,produce.getTel());
            preparedStatement.setString(8,produce.getName());
            preparedStatement.setString(9,String.valueOf(produce.getId()));
            if(preparedStatement.executeUpdate()==0){
                preparedStatement.close();
                return false;
            }
            preparedStatement.close();
        }
        catch (SQLException e){
            e.printStackTrace();
        }
        return true;
    }

    public String selectTypeName(int id){

        String str = null;

        DbUtils du = new DbUtils();
        Connection connection = du.getConn();

        ResultSet resultSet;

        String sql="select name from type where id=?";
        try {
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            if(!resultSet.next()){
                resultSet.close();
                preparedStatement.close();
                return null;
            }
            str =  resultSet.getString("name");
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return str;
    }
    public String selectStateName(int id){
        String str = null;

        DbUtils du = new DbUtils();
        Connection connection = du.getConn();

        ResultSet resultSet;

        String sql="select name from state where id=?";
        try {
            PreparedStatement preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            resultSet = preparedStatement.executeQuery();
            if(!resultSet.next()){
                resultSet.close();
                preparedStatement.close();
                return null;
            }
            str =  resultSet.getString("name");
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return str;
    }
}
